package com.xja.controller;

import com.github.pagehelper.PageInfo;
import com.xja.bean.*;
import com.xja.service.HouseService;
import com.xja.service.DistrictService;
import com.xja.service.HtypeService;
import com.xja.service.StreetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;


/**
 * @author 李岩
 * @date 2021/12/23-11:57
 * @describe
 */
@Controller
@RequestMapping("house")
public class HouseController {
    @Autowired
    private HouseService houseService;
    @Autowired
    private HtypeService htypeService;
    @Autowired
    private StreetService streetService;

    @Autowired
    private DistrictService districtService;

    //根据商品信息 详情
    @RequestMapping("detalis")
    public String detalis(House house,Model model){
        System.out.println("进入详情页");
        House house1 = houseService.selectByPrimaryKey(house.getId());
        model.addAttribute("house1",house1);
        return "details";
    }



    @RequestMapping("selectAllHouse")
    //查询所有的街道信息
    public String selectAllHouse(String pageNum, Model model,String title,String price,String sid,String typeId,String floorage) {
        System.out.println("controller pageNum=  "+pageNum+";title= "+title+";price= "+price+";sid= "+sid+";typeId= "+typeId+";floorage= "+floorage);
        Search search = new Search();
        search.setFloorage(floorage);
        search.setPrice(price);
        search.setSid(sid);
        search.setTitle(title);
        search.setTypeId(typeId);

        HashMap<String, String> map = new HashMap<>();
        map.put("title",title);
        if (!"不限".equals(sid)){
            map.put("sid",sid);
        }
        if (!"不限".equals(typeId)){
            map.put("typeId",typeId);
        }
        if (price!=null &&!"".equals(price) && !"不限".equals(floorage)){
            String[] split = price.split("-");
            map.put("minPrice",split[0]);
            map.put("maxPrice",split[1]);
            System.out.println("minPrice= "+map.get("minPrice")+";maxPrice= "+map.get("maxPrice"));
        }
        if (floorage!=null &&!"".equals(floorage) && !"不限".equals(floorage)){
            String[] flooragesplit = floorage.split("-");
            map.put("minFloorage",flooragesplit[0]);
            map.put("maxFloorage",flooragesplit[1]);
            System.out.println("minFloorage= "+map.get("minFloorage")+";maxFloorage= "+map.get("maxFloorage"));
        }
        try {
            PageInfo<House> pageInfo = houseService.selectAllHouse(pageNum,map);
            List<Htype> htypeList = htypeService.selectAllType();
            List<Street> streetList = streetService.selectAllStreet();
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("htypeList",htypeList);
            model.addAttribute("streetList",streetList);
            model.addAttribute("search",search);
            return "list.html";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error.html";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        model.addAttribute("HtypeList",htypeService.findAllHType());
        model.addAttribute("StreetList",streetService.findAllStreet());
        model.addAttribute("districtList",districtService.findAllDistrict());
        return "fabu";
    }

    @RequestMapping("addHouse")
    public String addHouse(MultipartFile img, House house, HttpServletRequest request, HttpSession session) throws IOException {
        if(!img.isEmpty()){
            String filename = img.getOriginalFilename();
            //设置文件的保存路径
            String realPath = request.getServletContext().getRealPath("/images");
            File file = new File(realPath);
            if(!file.exists()){
                file.mkdirs();
            }
            //设置文件的存储的名称
            String uuid = UUID.randomUUID().toString();
            //获取文件的后缀
            String suffix = filename.substring(filename.lastIndexOf("."));
            realPath+= File.separator+uuid+suffix;
            img.transferTo(new File(realPath));
            //文件的原始名字。 文件的真实存储名称uuid  文件的存储路径
            house.setImagename(uuid+suffix);
            //获取登录用户
            Users loginUser =(Users) session.getAttribute("users");
            house.setUserId(loginUser.getId());
            houseService.insertSelective(house);
        }
        //跳转到查询主页的控制器方法
        return "redirect:/house/selectAllHouse";
    }
    //根据商品信息 详情
    @RequestMapping("details")
    public String details(){
        return null;
    }

    //展示管理管理页面
    @RequestMapping("guanli")
    public String guanli(String pageNum, Model model){
        System.out.println("controller pageNum=  "+pageNum);
        try {
            PageInfo<House> pageInfo = houseService.selectAllHouse(pageNum);
            model.addAttribute("pageInfo",pageInfo);

            return "guanli";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error.html";
    }
    //管理页面删除租房信息
    @RequestMapping("deleteHouse")
    public String deleteHouse(Integer id){
        System.out.println("id= "+id);
        try {
            houseService.deleteByPrimaryKey(id);
            return "redirect:guanli";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }
    //管理页面修改租房信息
    @RequestMapping("toUpdateHouse")
    public String toUpdateHouse(Integer id,Model model){
        System.out.println("id= "+id);
        try {
            House house = houseService.selectByPrimaryKey(id);
            System.out.println(house);
            List<Htype> HtypeList = htypeService.selectAllType();
            List<Street> StreetList = streetService.selectAllStreet();
            List<District> districtList = districtService.findAllDistrict();
            model.addAttribute("house",house);
            model.addAttribute("HtypeList",HtypeList);
            model.addAttribute("StreetList",StreetList);
            model.addAttribute("districtList",districtList);
            return "update";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    //管理页面修改租房信息
    @RequestMapping("updateHouse")
    public String updateHouse(House house, District district, MultipartFile img, HttpServletRequest request, HttpSession session){
        try {
            System.out.println(house.getPubdate());

            if(!img.isEmpty()){
                String filename = img.getOriginalFilename();
                //设置文件的保存路径
                String realPath = request.getServletContext().getRealPath("/images");
                File file = new File(realPath);
                if(!file.exists()){
                    file.mkdirs();
                }
                //设置文件的存储的名称
                String uuid = UUID.randomUUID().toString();
                //获取文件的后缀
                String suffix = filename.substring(filename.lastIndexOf("."));
                realPath+= File.separator+uuid+suffix;
                img.transferTo(new File(realPath));
                //文件的原始名字。 文件的真实存储名称uuid  文件的存储路径
                house.setImagename(uuid+suffix);
                houseService.updateByPrimaryKeySelective(house);
            }
            return "redirect:guanli";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

}
